<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	/*if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}*/
	if(!empty($_RQ['mobile'])){
		$params['applyno']=$_RQ['applyno'];
		$return['params']['applyno']=$_RQ['applyno'];
	}
	//$fields=array('id','partnerlevel','partnertime','createtime','partnerstatus');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_supplier_clearing',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$merch=pdo_get('jy_supplier_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['merchid']),array('id','mid','merchname'));
		$v['merchname']=$merch['merchname'];
		$v['mid']=$merch['mid'];
		$v['applytime']=date('Y/m/d H:i:s',$v['applytime']);
		$v['checktime']=date('Y/m/d H:i:s',$v['checktime']);
		
	}
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已结算','3'=>'已拒绝');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'提现管理'),			
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'clearno','title'=>'提现单号','type'=>'string'),
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'merchid','title'=>'商户ID','type'=>'string'),
			array('name'=>'mid','title'=>'绑定会员ID','type'=>'string'),
			array('name'=>'realprice','title'=>'申请金额','type'=>'string'),
			array('name'=>'realpricerate','title'=>'抽成后金额','type'=>'string'),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string'),				
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),	
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),	
		),
		'action'=>array(
			array('op'=>'check','title'=>'审核','type'=>'modal','show'=>array('status'=>0)),
			array('type'=>'modal','op'=>'pay','title'=>'打款','show'=>array('status'=>1)),
		),
		),
		'search'=>array(
			
			array('name'=>'clearno','width'=>15,'title'=>'提现单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='check'){
	$id=intval($_RQ['id']);
	$return['status']=array(0=>'待审核',1=>'审核通过',2=>'已结算',3=>'拒绝');
	$return['item']=pdo_get('jy_supplier_clearing',array('id'=>$id));
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
			array('name'=>'realprice','title'=>'申请金额','type'=>'string','read'=>true),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string','read'=>true),
			array('name'=>'pay_typetitle','title'=>'打款方式','type'=>'string','read'=>true),			
			array('type'=>'copy','name'=>'realname','title'=>'真实姓名','show'=>array('pay_type'=>array('alipay','union'))),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝帐号','show'=>array('pay_type'=>array('alipay'))),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('pay_type'=>array('union'))),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('pay_type'=>array('union'))),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('type'=>'text','title'=>'申请事由','name'=>'content','read'=>true),
			)
		);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$post=get_postdata();
	pdo_update('jy_supplier_clearing',array('status'=>$post['status']),array('id'=>$post['id']));
	exi('审核成功','','list');
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$type=$post['type'];
		$status=intval($post['status']);
		if(!$status){
			exi('请先选择是否审核通过！','error');
		}
		if($status==1){
			$params=array('money'=>$post['finalprice'],'remark'=>'多商户提现');
			$this->model('commission')->pay_clearing($id,$post['pay_type']);
			exi('打款成功！','','list');
		}else{
			if(!$post['remark']){
				exi('请填写审核不通过原因！','error');
			}
			pdo_update('jy_supplier_clearing',array('status'=>3,'remark'=>trim($post['remark'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			exi('操作成功！','','list');
		}
	}
	$item=pdo_get('jy_supplier_clearing',array('id'=>intval($_RQ['id'])));
	$item['mid']=pdo_getcolumn('jy_supplier_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),'mid');
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	if($item['status']==2){
		exi('该提现已完成，请勿重复操作~','error');
	}
	$return['item']=$item;
	$return['item']['pay_typetitle']=$return['item']['pay_type']?$this->pay_types[$return['item']['pay_type']]:'';
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'打款',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，必须选择打款《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('type'=>'string','name'=>'finalprice','title'=>'打款金额','read'=>true),
			array('name'=>'pay_typetitle','title'=>'打款方式','type'=>'string','read'=>true),			
			array('type'=>'copy','name'=>'realname','title'=>'真实姓名','show'=>array('pay_type'=>array('alipay','union'))),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝帐号','show'=>array('pay_type'=>array('alipay'))),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('pay_type'=>array('union'))),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('pay_type'=>array('union'))),
			array('type'=>'radio','name'=>'status','title'=>'审核','options'=>'statuss'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'string','show'=>array('status'=>2)),
			array('type'=>'text','title'=>'申请事由','name'=>'content','read'=>true),
		),
	);
	$return['statuss']=array(
		'1'=>'审核通过并打款',
		'2'=>'审核不通过',
	);
	$return['wxapps']=pdo_getall('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_supplier_clearing',array('ispartner'=>0,'partnerstatus'=>0),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_supplier_clearing',array('ispartner'=>0,'partnerstatus'=>0),array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>